package com.terrace.web.tagext.easyui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.core.constant.ConfigConstant;
import com.terrace.core.utils.JsonUtils;
import com.terrace.web.constant.EasyuiConstant;
import com.terrace.web.tagext.easyui.model.GridColumn;
import com.terrace.web.tagext.easyui.support.DataGridTagSupport;
import com.terrace.web.tagext.easyui.utils.EasyUIUtils;

/**
 * 组合网格标签
 * 
 * @author jiangyg
 *
 */
public class ComboGridTag extends DataGridTagSupport {

	private static final long serialVersionUID = 1415601324491335323L;

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ComboGridTag.class);

	/**
	 * 定义是否字段应被输入
	 */
	private boolean required = false;
	
	/**
	 * 组合宽度
	 */
	private Integer width;

	/**
	 * 组合高度
	 */
	private Integer height;

	/**
	 * 组合下拉面板宽度
	 */
	private Integer panelWidth;

	/**
	 * 组合下拉面板高度
	 */
	private Integer panelHeight;
	
	/**
	 * 是否显示页面列表
	 */
	private boolean showPageList = false;
	
	/**
	 * 是否可以编辑--默认不可编辑
	 */
	private boolean editable = false;

	/**
	 * 是否禁用
	 */
	private boolean disabled = false;

	/**
	 * 是否只读
	 */
	private boolean readonly = false;
	
	/**
	 * 是否多选
	 */
	private boolean multiple = false;
	
	/**
	 * 显示内容字段
	 */
	private String textField;
	
	/**
	 * 默认单个选项--单选时用到
	 */
	private String setValue;
	
	/**
	 * 默认多个选项--多选时用到
	 */
	private String[] setValues;
	
	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(this.bulidGrid());
		} catch (Exception e) {
			logger.error("数据字典组合框生成失败：", e);
		}
		return EVAL_PAGE;
	}
	
	@Override
	public String bulidGrid() {
		StringBuffer sb = new StringBuffer();
		sb.append("<select id=\"" + name + "\" name=\"" + name + "\" class=\"easyui-" + gridType() + "\" ");
		
		sb.append("data-options=\"");
		EasyUIUtils.appendDatsOption(sb, "url", action);
		EasyUIUtils.appendDatsOption(sb, "toolbar", "#" + name + "ToolBar");
		EasyUIUtils.appendDatsOption(sb, "required", required);
		EasyUIUtils.appendDatsOption(sb, "disabled", disabled);
		EasyUIUtils.appendDatsOption(sb, "readonly", readonly);
		EasyUIUtils.appendDatsOption(sb, "multiple", multiple);
		EasyUIUtils.appendDatsOption(sb, "editable", editable);
		EasyUIUtils.appendDatsOption(sb, "width", width);
		EasyUIUtils.appendDatsOption(sb, "height", height);
		EasyUIUtils.appendDatsOption(sb, "panelWidth", panelWidth);
		EasyUIUtils.appendDatsOption(sb, "panelHeight", panelHeight);
		EasyUIUtils.appendDatsOption(sb, "mode", "remote");
		EasyUIUtils.appendDatsOption(sb, "idField", idField);
		EasyUIUtils.appendDatsOption(sb, "textField", textField);
		EasyUIUtils.appendDatsOption(sb, "pageNumber", pageNumber);
		EasyUIUtils.appendDatsOption(sb, "pageSize", pageSize);
		EasyUIUtils.appendDatsOption(sb, "showPageList", showPageList);
		EasyUIUtils.appendDatsOption(sb, "pageList", new int[] { pageSize, pageSize * 2, pageSize * 3, pageSize * 4 });
		EasyUIUtils.appendDatsOption(sb, "pagination", pagination);
		EasyUIUtils.appendDatsOption(sb, "loadMsg", ConfigConstant.value("easyui.combobox.loadMsg"));
		
		// 列属性生成
		sb.append("columns:[[" + container.transferColumnsToJSObject(false).replace("\"", "\\\'") + "]],");
		sb.append("frozenColumns:[[" + container.transferColumnsToJSObject(true).replace("\"", "\\\'") + "]],");

		EasyUIUtils.substringLastComma(sb);
		sb.append("\"></select>");
		
		sb.append(this.createGridToolBar());
		
		// 生成表格js
		sb.append("<script type=\"text/javascript\">");
		if (StringUtils.isNotBlank(setValue) || (setValues != null && setValue.length() > 0)) {
			sb.append("$(function() {");
			if (StringUtils.isNotBlank(setValue)) {
				sb.append("$('#" + name + "').combogrid('setValue', '" + setValue + "');");
			} else {
				sb.append("$('#" + name + "').combogrid('setValues', " + JsonUtils.beanToJson(setValues) + ");");
			}
			sb.append("");
			sb.append("});");
		}
		sb.append(this.createGridCommonUseFuncs());
		sb.append("</script>");
		
		return sb.toString();
	}

	@Override
	public String createGridToolBar() {
		StringBuffer sb = new StringBuffer();
		// 如果没有工具按钮和查询列，则不需要生成工具条
		if (container.getToolbars().size() == 0 && !container.hasQueryColumn()) {
			return "";
		}
		sb.append("<div id=\"" + name + "ToolBar\" style=\"padding: 5px; height: auto;\">");
		
		// 查询列生成
		sb.append("<div>");
		for (GridColumn column : container.getColumns()) {
			if (column.isQuery()) {
				sb.append("<span class=\"easyui-query-box\">");
				sb.append("<span class=\"easyui-query-title\" title=\"" + column.getTitle() + "\">&nbsp;" + column.getTitle() + "：</span>");
				// 查询列是字典的话，生成下拉框，否则默认生成文本框
				if (StringUtils.isNotBlank(column.getDictionary())) {
					sb.append("<input id=\"" + column.getField() + "\" name=\"" + column.getField() + "\" type=\"text\" class=\"easyui-combobox\" ");
					sb.append("data-options=\"");
					EasyUIUtils.appendDatsOption(sb, "valueField", "dataValue");
					EasyUIUtils.appendDatsOption(sb, "textField", "dataName");
					EasyUIUtils.appendDatsOption(sb, "url", EasyuiConstant.message("data.dict.code.url", column.getDictionary()));
					sb.append("\"");
					sb.append(">");
				} else {
					sb.append("<input class=\"easyui-textbox\" type=\"text\" name=\"" + column.getField() + "\" />");
				}
				sb.append("</span>");
			}
		}
		
		// 搜索和重置按钮
		sb.append("<span style=\"float: right;\">");
		sb.append("<a href=\"#\" class=\"easyui-linkbutton\" iconCls=\"icon-standard-magnifier\" onclick=\"" + name + "Search()\">查询</a>");
		sb.append("</span>");
		
		sb.append("</div>");
		
		
		
		sb.append("</div>");
		return sb.toString();
	}
	
	@Override
	public String createGridCommonUseFuncs() {
		StringBuffer sb = new StringBuffer();
		
		// 表格查询函数
		sb.append("function " + name + "Search() {var queryParams = $('#" + name + "')." + gridType() + "('options').queryParams;");
		sb.append("$('#" + name + "ToolBar').find('*').each(function() {if($(this).attr('name')){queryParams[$(this).attr('name')] = $(this).val();}});");
		sb.append("$('#" + name + "')." + gridType() + "({url : '" + action + "',pageNumber : 1});}");
		
		return sb.toString();
	}
	
	@Override
	public String assembleExtendDataOptions() {
		return null;
	}

	@Override
	public String gridType() {
		return "combogrid";
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getTextField() {
		return textField;
	}

	public void setTextField(String textField) {
		this.textField = textField;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getPanelWidth() {
		return panelWidth;
	}

	public void setPanelWidth(Integer panelWidth) {
		this.panelWidth = panelWidth;
	}

	public Integer getPanelHeight() {
		return panelHeight;
	}

	public void setPanelHeight(Integer panelHeight) {
		this.panelHeight = panelHeight;
	}

	public boolean isShowPageList() {
		return showPageList;
	}

	public void setShowPageList(boolean showPageList) {
		this.showPageList = showPageList;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}

	public String getSetValue() {
		return setValue;
	}

	public void setSetValue(String setValue) {
		this.setValue = setValue;
	}

	public String[] getSetValues() {
		return setValues;
	}

	public void setSetValues(String[] setValues) {
		this.setValues = setValues;
	}

}
